package droidninja.filepicker;

import android.content.pm.ActivityInfo;
import android.os.Bundle;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import droidninja.filepicker.load.LoadingDialog;
import droidninja.filepicker.utils.Orientation;

/**
 * Created by droidNinja on 22/07/17.
 */

public abstract class BaseFilePickerActivity extends AppCompatActivity {
    @Nullable
    private ThreadPoolExecutor mDiskIO;
    @Nullable
    private LoadingDialog mProgressDialog;

    protected void onCreate(@Nullable Bundle savedInstanceState, @LayoutRes int layout) {
        super.onCreate(savedInstanceState);
        setTheme(PickerManager.getInstance().getTheme());
        setContentView(layout);

        //set orientation
        Orientation orientation = PickerManager.getInstance().getOrientation();
        if (orientation == Orientation.PORTRAIT_ONLY)
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        else if (orientation == Orientation.LANDSCAPE_ONLY)
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        initView();
    }

    protected abstract void initView();


    @NonNull
    protected LoadingDialog getProgressDialog() {
        if (mProgressDialog == null) {
            mProgressDialog = new LoadingDialog(new android.app.ProgressDialog(this));
        }
        return mProgressDialog;
    }

    /**
     * 显示加载进度
     */
    public void showLoading() {
        showLoading("正在加载...");
    }

    /**
     * 显示加载进度
     */
    public void showLoading(String title) {
        showLoading(title, true);
    }

    /**
     * 设置点击弹窗外面是否关闭弹窗，默认为true
     *
     * @param title 要显示的文字
     */
    public void showLoading(String title, boolean isDismissOutside) {
        showLoading(title, isDismissOutside, isDismissOutside);
    }

    public void showLoading(@StringRes int titleId) {
        showLoading(getResources().getString(titleId));
    }

    public void showLoading(@NonNull String title, boolean isDismissOnBackPressed,
                            boolean isDismissOnTouchOutside) {
        if (mProgressDialog == null) {
            getProgressDialog().showLoading(title, isDismissOnBackPressed, isDismissOnTouchOutside);
            return;
        }
        mProgressDialog.showLoading(title, isDismissOnBackPressed, isDismissOnTouchOutside);
    }

    public void hideLoading() {
        if (mProgressDialog == null) {
            return;
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mProgressDialog == null) {
                    return;
                }
                mProgressDialog.hideLoading();
            }
        });
    }

    protected ThreadPoolExecutor getIo() {
        if (mDiskIO == null) {
            int CPU_COUNT = Runtime.getRuntime().availableProcessors();
            int CORE_POOL_SIZE = Math.max(4, Math.min(CPU_COUNT + 1, 5));
            int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
            return mDiskIO = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE,
                    30L, TimeUnit.SECONDS,
                    new PriorityBlockingQueue<Runnable>(),
                    new ThreadFactory() {
                        private static final String THREAD_NAME_STEM = "hos_io_%d";

                        private final AtomicInteger mThreadId = new AtomicInteger(0);

                        @Override
                        public Thread newThread(Runnable r) {
                            Thread t = new Thread(r);
                            t.setName(String.format(THREAD_NAME_STEM, mThreadId.getAndIncrement()));
                            return t;
                        }
                    });
        }

        return mDiskIO;
    }

    public int getLeastCompressSize() {
        return PickerManager.getInstance().getLeastCompressSize();
    }

    @Override
    protected void onDestroy() {
        if (mProgressDialog != null) {
            mProgressDialog.onDestroy();
        }
        if (mDiskIO != null) {
            mDiskIO.shutdown();
            mDiskIO = null;
        }
        super.onDestroy();
    }
}
